<?php
namespace App\Service\Admin;

class WorkhourService0910 extends BaseService
{
    //数据列表
    public static function index($pas)
    {
        $db = self::dbConn(self::$dbConfigWorkHour)->where('del', 0);
        if (isset($pas['role_id']) && $pas['role_id']) {
            $db = $db->where('role_id', $pas['role_id']);
        }
        if (isset($pas['depart_id']) && $pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        $res = $db->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $types = self::$whTypes;
        $arys = [];
        foreach ($res as $v) {
            $type_name = array_key_exists($v->type, $types) ? $types[$v->type] : '';
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'type' => $v->type, 'type_name' => $type_name,
                'lasttime' => $v->uptime,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //选项
    public static function getOption()
    {
        $arys = ['type' => []];
        $types = self::$whTypes;
        foreach ($types as $k => $v) {
            $arys['type'][] = [
                'id' => $k, 'title' => $v,
            ];
        }
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigWorkHour)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbConfigWorkHour)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbConfigWorkHour)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}